package com.yy.permissions_general.module.system.file.controller;

import com.yy.permissions_general.common.aopSubmit.aop.NoRepeatSubmit;
import com.yy.permissions_general.module.system.aopLog.aop.MyLog;
import com.yy.permissions_general.module.system.file.entity.File;
import com.yy.permissions_general.module.system.file.service.FileService;
import com.yy.permissions_general.utils.ResultUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 附件前端控制器
 * </p>
 *
 * @author YY
 * @since 2021-12-20
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
    * 查询列表
    * @param file
    * @param limit
    * @param page
    * @return
    */
    @MyLog("查询列表")
//    @NoRepeatSubmit(key="file.list")
    @RequestMapping("/list")
    public ResultUtil list(File file, @RequestParam(value = "limit",defaultValue = "999999")int limit, @RequestParam(value = "page",defaultValue = "0")int page) {
      return ResultUtil.buildPagination(fileService.selectPageList(file,limit,page));
    }

    /**
    * 新建文件夹
    * @param file
    * @return
    */
    @MyLog("新建文件夹")
    @NoRepeatSubmit(key="file.addFolder")
    @PreAuthorize("hasPermission('/file/addFolder','file:addFolder')")
    @RequestMapping("/addFolder")
    public ResultUtil addFolder(File file) {
        fileService.addFolder(file);
        return ResultUtil.success();
    }
    /**
    * 重命名文件夹
    * @param file
    * @return
    */
    @MyLog("重命名文件夹")
    @NoRepeatSubmit(key="file.editFolder")
    @PreAuthorize("hasPermission('/file/editFolder','file:editFolder')")
    @RequestMapping("/editFolder")
    public ResultUtil editFolder(File file) {
        fileService.editFolder(file);
        return ResultUtil.success();
    }
    /**
    * 新增数据
    * @param file
    * @return
    */
    @MyLog("新增数据")
    @NoRepeatSubmit(key="file.addFile")
    @PreAuthorize("hasPermission('/file/addFile','file:addFile')")
    @RequestMapping("/addFile")
    public ResultUtil addFile(@RequestParam(value = "file",required = true) MultipartFile[] multipartFile,File file) {
        fileService.addFile(multipartFile,file);
        return ResultUtil.success();
    }

    /**
    * 修改数据
    * @param file
    * @return
    */
    @MyLog("修改数据")
    @NoRepeatSubmit(key="file.editFile")
    @PreAuthorize("hasPermission('/file/editFile','file:editFile')")
    @RequestMapping("/editFile")
    public ResultUtil editFile(File file) {
        fileService.editFile(file);
        return ResultUtil.success();
    }

    /**
    * 查看详情
    * @param id
    * @return
    */
    @MyLog("查看详情")
    @RequestMapping("/detailFile")
    public ResultUtil detailFile(Integer id) {
        File file = fileService.getDetailsById(id);
        return ResultUtil.success(file);
    }

    /**
    * 根据ID删除记录
    * @param id
    * @return
    */
    @MyLog("删除文件")
    @NoRepeatSubmit(key="file.delete")
    @PreAuthorize("hasPermission('/file/delete','file:delete')")
    @RequestMapping("/delete")
    public ResultUtil delete(Integer id) {
        fileService.deleteById(id);
        return ResultUtil.success();
    }

    /**
    * 批量删除数据
    * @param ids ID集合
    * @return
    */
    @MyLog("批量删除")
    @NoRepeatSubmit(key="file.deleteBatch")
    @PreAuthorize("hasPermission('/file/deleteBatch','file:deleteBatch')")
    @RequestMapping("/deleteBatch")
    public ResultUtil deleteBatch(@RequestParam(value = "ids[]",required = true) List<Long> ids) {
        fileService.deleteBatch(ids);
        return ResultUtil.success();
    }

    /**
     * 查询目录结构
     * @param parentId 父ID
     * @return
     */
    @RequestMapping("/getFileTree")
    @PreAuthorize("hasPermission('/file/getFileTree','file:getFileTree')")
    @MyLog("查询目录结构")
    public ResultUtil getFileTree(Integer parentId) {
        return ResultUtil.buildPagination(fileService.getFileTree(parentId));
    }

}
